package bcMedEntity;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Gerencia conexao com o Banco de Dados
 *
 */
public class EntityDataBase {

	final private String jdbcDriverName = "com.mysql.jdbc.Driver";
	final private String dataBaseLocation = "localhost/bcmed_v02?user=root&password=bentes123";
	
	
	private Connection dbConnection;
	
	public void connectDataBase(){
		try{		
			dbConnection = DriverManager.getConnection("jdbc:mysql://"+dataBaseLocation);
		}catch (Exception e) {
			System.out.println(">>> Exception at EntityDataBase:connectDataBase:" + e.getMessage());
			dbConnection = null;
		}
		return;
	}
	
	
	public boolean isDriverPresent(){
		try{
			Class.forName(jdbcDriverName).newInstance();
			return true;
		}catch (Exception e) {
			System.err.println(">>> Unable to find and load driver");
			return false;
		}
	}
	
	
	public void executeUpdate(String sql){
		try{			
			Statement statement = dbConnection.createStatement();
			statement.executeUpdate(sql);
		}catch (Exception e) {
			System.out.println(">>> Exception at EntityDataBase:executeStatement : "+e.getMessage());
		}
		return;
	}

	
	public ResultSet executeQuery(String sql){
		ResultSet result = null;
		try{
			Statement statement = dbConnection.createStatement();
			result = statement.executeQuery(sql);
		}catch (Exception e) {
			System.out.println(">>> Exception at EntityDataBase:executeStatement : "+e.getMessage());
		}
		return result;
	}
}
